package io.github.talelin.latticy.controller.api;

import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.LoginMeta;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.bo.PageCounterBO;
import io.github.talelin.latticy.common.CommonUtil;
import io.github.talelin.latticy.dto.JobDTO;
import io.github.talelin.latticy.entity.Job;
import io.github.talelin.latticy.entity.User;
import io.github.talelin.latticy.service.JobService;
import io.github.talelin.latticy.vo.*;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/api/job")
@Validated
public class JobController {

    @Autowired
    private JobService jobService;

    /**
     * 发布任务
     */
    @PostMapping
    @LoginRequired
    public CreatedVO create(@RequestBody @Validated JobDTO dto) {
        jobService.create(dto);
        return new CreatedVO<>();
    }

    /**
     * 备注任务
     */
    @PutMapping("/{id}")
    @LoginRequired
    public UpdatedVO update(@RequestBody @Validated JobDTO dto,
                       @PathVariable @Positive Long id) {
        jobService.update(dto, id);
        return new UpdatedVO<>();
    }

    /**
     * 获取任务
     */
    @GetMapping("/{id}")
    @LoginRequired
    public Job get(@PathVariable @Positive Long id) {
        return jobService.get(id);
    }

    /**
     * 设置任务是否完成
     */
    @GetMapping("/{id}/setting")
    @LoginRequired
    public UpdatedVO setting(@PathVariable @Positive Long id) {
        jobService.updateStatus(id);
        return new UpdatedVO<>();
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/{id}")
    @LoginRequired
    public DeletedVO delete(@PathVariable @Positive Long id) {
        jobService.delete(id);
        return new DeletedVO<>();
    }

    /**
     * 任务
     */
    @GetMapping("/day")
    @LoginRequired
    public JobByDateVO today(@RequestParam String time,
                             @RequestParam(name = "user_id", defaultValue = "0", required = false) Long userId) {
        return jobService.getByDate(time, userId);
    }

    /**
     * 最近有未完成任务的日期
     */
    @GetMapping("/lasttime")
    @LoginRequired
    public String lastTime() {
        return jobService.getLastTime();
    }

    /**
     * 日志薄
     */
    @GetMapping("/page")
    @LoginRequired
    public JobPageVO page(@RequestParam(defaultValue = "0", required = false)
                     @Min(value = 0) Integer start,
                          @RequestParam(defaultValue = "20", required = false)
                     @Min(value = 1)
                     @Max(value = 30) Integer count) {
        PageCounterBO pageCount = CommonUtil.converToPageParamter(start, count);
        return jobService.getJobs(pageCount.getPage(), pageCount.getSize());
    }

    /**
     * 其他同事任务
     */
    @GetMapping("/team")
    @LoginRequired
    public List<TeamJobVO> team() {
        return jobService.getTeamJob();
    }




}
